package com.ehotting.edsta.simulation.aop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ehotting.edsta.simulation.controller.SimulationWsController;
import com.ehotting.mqbd.zw.api.constants.ActionConstants;
import com.ehotting.mqbd.zw.api.domain.netty.ZwCommPacket;
import com.ehotting.mqbd.zw.client.aop.AbstractZwMqActionAspect;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class ZwMqActionAspect extends AbstractZwMqActionAspect {

    @Autowired
    private SimulationWsController simulationWsController;

    @Pointcut("execution(* com.ehotting.mqbd.zw.client.mqAction.*.invoke(..))")
    public void messageAction(){

    }

    @Around(value = "messageAction() && args(request) ", argNames = "pjp,request")
    public Object aroundMessageAction(ProceedingJoinPoint pjp, ZwCommPacket request) throws Throwable{
        Object response  = pjp.proceed(new Object[]{request});
        sendToQueue(request);
        return response;
    }

    public void sendToQueue(ZwCommPacket request) {
        short cmd = request.getHead().getCmd();
        if (cmd == ActionConstants.Cmd_ClusterInfoQuery.getZwCommandVal()){
            return;
        }

        log.info("Send[agentId:{}] cmd {}",request.getFrom(),cmd);
        try {
            simulationWsController.sendUserMessage(JSON.toJSONString(request, SerializerFeature.DisableCircularReferenceDetect),request.getFrom());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
